#include "main.h"
#include "user_usart.h"
#include "./Letter_Shell/shell.h"
#include "./Letter_Shell/letter_shell_porting.h"

/*全局变量定义*/
lwq_t debug_data_que; /*调试数据缓冲*/

/*静态变量定义*/
static Shell debug_usart_shell; /*依赖于调试串口的shell*/
static char shell_buffer[SHELL_BUFFER_LEN]; /*shell工作缓冲区*/

/*静态函数声明*/
static short shellWrite(char *data, unsigned short len);

/*********静态函数定义**********/

/*shell发送函数*/
static short shellWrite(char *data, unsigned short len)
{
	DL_UART_Transmit(DEBUG_USART_INST, (uint8_t *)data, (uint16_t)len); /*串口阻塞式发送，避免中断冲突*/	
	return len;
}

/********全局函数定义***********/

/*shell初始化函数*/
void debug_shell_init(void)
{	
	lwq_create(&debug_data_que, DEBUG_QUE_LEN, sizeof(char)); /*创建调试数据缓冲区*/
    debug_usart_shell.write = shellWrite; /*绑定串口发送函数*/
    shellInit(&debug_usart_shell, shell_buffer, SHELL_BUFFER_LEN); /*初始化shell对象*/
}

/*时间片轮询机制的shell任务函数*/
void shell_task_handler(void)
{
	char shell_rec_err;
	char shell_rec_data;
	
	shell_rec_err = lwq_read(&debug_data_que, &shell_rec_data);
	
	/*如果成功接收到数据*/
	if(shell_rec_err != 0)
	{					
		shellHandler(&debug_usart_shell, shell_rec_data);
	}	
}
